package com.x.erp.dao;

import com.x.erp.model.MultilevelActivityBond;
import com.x.framework.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 多级营销活动保证金信息DAO
 */
@Repository
public class MultilevelActivityBondDao extends BaseDao {

	private final static String selectMultilevelActivityBondListSql = "SELECT " +
			"emab.* " +
			"FROM " +
			"erp_multilevel_activity_bond emab " +
			"WHERE " +
			"1 = 1 ";
	/**
	 * 查询多级营销活动保证金列表
	 * @param multilevelActivityBond
	 * @return
	 * @throws Exception
	 */
	public List<MultilevelActivityBond> selectMultilevelActivityBondList(MultilevelActivityBond multilevelActivityBond) throws Exception{
		StringBuilder sql = new StringBuilder(selectMultilevelActivityBondListSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql, params, multilevelActivityBond);
		return this.queryForList(sql.toString(), MultilevelActivityBond.class, params, multilevelActivityBond);
	}

	private final static String selectMultilevelActivityBondNumSql = "SELECT COUNT(emab.BOND_ID) FROM erp_multilevel_activity_bond emab WHERE 1=1 ";
	/**
	 * 查询多级营销活动保证金数量
	 * @param multilevelActivityBond
	 * @return
	 * @throws Exception
	 */
	public int selectMultilevelActivityBondNum(MultilevelActivityBond multilevelActivityBond) throws Exception{
		StringBuilder sql = new StringBuilder(selectMultilevelActivityBondNumSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql, params, multilevelActivityBond);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 查询条件
	 * @param sql
	 * @param params
	 * @param multilevelActivityBond
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, MultilevelActivityBond multilevelActivityBond) {
		if(multilevelActivityBond != null) {
			if(StringUtils.isNotEmpty(multilevelActivityBond.getBondId())){
				sql.append("AND emab.BOND_ID = ? ");
				params.add(multilevelActivityBond.getBondId());
			}
			if(StringUtils.isNotEmpty(multilevelActivityBond.getShopId())){
				sql.append("AND emab.SHOP_ID = ? ");
				params.add(multilevelActivityBond.getShopId());
			}
		}
	}
}
